package com.example.campuslink.commen.db.invitation

import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import androidx.room.TypeConverters
import com.example.campuslink.commen.db.ListCommentTableConverters
import com.example.campuslink.commen.db.ListListCommentTableConverter
import com.example.campuslink.commen.db.UserInfoDataConverter
import com.example.campuslink.commen.db.comment.CommentTable
import com.example.campuslink.commen.db.userInfo.UserInfoTable
import com.example.campuslink.commen.db.userNumber.ChildrenDataConverter
import com.example.campuslink.commen.db.userNumber.ListCommendInfoConverters
import com.example.campuslink.model.ChildrenCommendData
import com.example.campuslink.model.CommendInfoData
import db.ListStringConverter
import java.io.Serializable

@Entity(tableName = "invitation_table", primaryKeys = ["id", "userNumber"])
@TypeConverters(
    ListStringConverter::class,
    ListCommendInfoConverters::class,
    UserInfoDataConverter::class,
    ChildrenDataConverter::class
)
data class InvitationTable(
    var id: Int = 0,
    var userNumber: String = "",
    var invitationTitle: String = "",
    var invitationContent: String = "",
    var invitationCreateTime: Long = 0,
    var commentCount: Int = 0,
    var invitationType: String = "",
    @TypeConverters(ListStringConverter::class)
    var collectionThisInvitationNumber: MutableList<String> = mutableListOf(),
    @TypeConverters(UserInfoDataConverter::class)
    var userInfoTable: UserInfoTable? = null,
    @TypeConverters(ListStringConverter::class)
    var invitationImage: MutableList<String> = mutableListOf(),
    @TypeConverters(ListCommendInfoConverters::class)
    var invitationParentCommend:MutableList<CommendInfoData> = mutableListOf(),
    @TypeConverters(ChildrenDataConverter::class)
    var invitationChildrenCommend:ChildrenCommendData = ChildrenCommendData()
) : Serializable {
    @Ignore
    constructor() : this(
        id = 0,
        userNumber = "",
        invitationTitle = "",
        invitationContent = "",
        invitationCreateTime = 0,
        commentCount = 0,
        invitationType = "",
        collectionThisInvitationNumber = mutableListOf(),
        userInfoTable = null,
        invitationImage = mutableListOf(),
        invitationParentCommend = mutableListOf(),
        invitationChildrenCommend = ChildrenCommendData()
    )

    companion object {
        suspend fun insertWithId(
            dao: InvitationDao,
            invitation: InvitationTable,
            userNumber: String // 明确传入 userNumber
        ) {
            val maxId = dao.getMaxId() ?: 0
            val newInvitation = invitation.copy(id = maxId + 1, userNumber = userNumber)
            dao.insertInvitation(newInvitation)
        }
    }
}
